package cn.blue.system.service.impl;

import cn.blue.system.domain.Role;
import cn.blue.system.mapper.MenuMapper;
import cn.blue.system.mapper.PermissionMapper;
import cn.blue.system.mapper.RoleMapper;
import cn.blue.system.service.IRoleService;
import cn.blue.base.service.impl.BaseServiceImpl;
import org.mockito.internal.matchers.Find;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.sql.DataSourceDefinition;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hui
 * @since 2022-10-24
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private MenuMapper menuMapper;



    @Override
    public void add(Role role) {
        //保存角色信息 需要反回主键 用于保存中间表
       roleMapper.add(role);
       //添加中间表
       permissionMapper.addMiddle(role.getId(),role.getPermissions());
       menuMapper.addMiddle(role.getId(),role.getMenus());

    }

    @Override
    @Transactional
    public void update(Role role) {
        super.update(role);
        //删除菜单中间表
        menuMapper.deleteMiddle(role.getId());
        //删除权限中间表
        permissionMapper.deleteMiddle(role.getId());
        //添加按钮权限
        permissionMapper.addMiddle(role.getId(), role.getPermissions());
        //添加菜单权限
        menuMapper.addMiddle(role.getId(), role.getMenus());

    }

    @Override
    public void del(Long id) {
        super.del(id);
        //删除菜单中间表
        menuMapper.deleteMiddle(id);
        permissionMapper.deleteMiddle(id);

    }
}
